#include "show.h"
#include "cmsis_os2.h"
#include "oled.h"
#include "main.h"
#include "FreeRtos.h"
#include "semphr.h"
extern uint8_t menu;
uint8_t curmenu=0;
extern info ttinfo;
extern SemaphoreHandle_t semhandle;
uint16_t oldlight=0;
timeinfo pretime={0};
uint16_t get_len(uint16_t num)
{   uint16_t len=0;
   while(num)
	 {
	    len++;
		  num/=10;

	 }
		return len; 

}
void adjustlight(int level)
{
     OLED_WR_CMD(0x81);
     switch(level)
		 {
		   case 1:
			  OLED_WR_CMD(0x0f);	 
			 break;
			 case 2:
				OLED_WR_CMD(0x8f); 
			 break;
			 case 3:
				OLED_WR_CMD(0xff); 
		   break;

		 }

}
void show_Envriment()
{     
	
	
	   xSemaphoreTake(semhandle,portMAX_DELAY);
	   uint16_t light=ttinfo.light;
	   uint8_t tmp=ttinfo.tmp;
	   uint8_t hum=ttinfo.hum;
     xSemaphoreGive(semhandle);
        
	   if(light<200)	
		 {
		   adjustlight(1); 
		 
		 }		
     else if(light<1000)	
		 {
		 
		    adjustlight(2); 
		 
		 }			
     else
     {
		    adjustlight(3); 
		 
		 
		 }			 
	
	
	
	
	for(int i=0;i<16;i++)
	{
	   OLED_ShowChar(i*8,4,'-',16);

	}
	
	uint16_t len=get_len(light);

	if(oldlight!=light||curmenu!=menu)
	{	
		for(int i=0;i<4;i++)
	{
	
	  OLED_Clearrow(i);
	
	}
	OLED_ShowNum((8-len)/2*16,0,light,len,32);
	OLED_Clearrow(6);
	OLED_Clearrow(7);
	
	OLED_ShowChinese(40,6,10);
	OLED_ShowChar(90,6,'%',16);
	
	OLED_ShowNum(24,6,tmp,2,16);
  OLED_ShowNum(74,6,hum,2,16);
  } 
	
	    oldlight=light;
	   
}
void show_time()
{   if(curmenu!=menu)  //固定的
    { OLED_Clear(); 
	  OLED_ShowChinese(16*2,0,11);
    OLED_ShowChinese(16*3,0,12);
    OLED_ShowChinese(16*4,0,13);
    OLED_ShowChinese(16*5,0,14);
	
	  OLED_ShowChinese(80,2,15);
    OLED_ShowChinese(96,2,16);
		}
		 
		uint32_t curtime=ttinfo.time+3600*8;
    struct tm*t=localtime(&curtime);		
		uint32_t year=t->tm_year+1900;
		uint8_t month=t->tm_mon+1;
		uint8_t day=t->tm_mday;
		uint8_t hour=t->tm_hour;
		uint8_t min=t->tm_min;
		uint8_t second=t->tm_sec;
		uint8_t week=t->tm_wday;
		uint8_t  x_pos=0;
	  if(year!=pretime.year||curmenu!=menu)
		{
		
		  OLED_ShowNum(x_pos,2,year,4,16);
		
		
		}
		x_pos+=32;
		OLED_ShowChar(x_pos,2,'-',16);
		x_pos+=8;
		 if(month!=pretime.month||curmenu!=menu)
		{
		
		  OLED_ShowNum(x_pos,2,month,get_len(month),16);
		
		
		}
		if(get_len(month)==1)
		{x_pos+=8;}
		else
		{x_pos+=16;}
		OLED_ShowChar(x_pos,2,'-',16);
		x_pos+=8;
			 if(day!=pretime.day||curmenu!=menu)
		{
		
		  OLED_ShowNum(x_pos,2,day,get_len(day),16);
		
		
		}
		 if(get_len(day)==1)
		{x_pos+=8;}
		 else
		{x_pos+=16;}
	   OLED_ShowChinese(112,2,week+16);
		 x_pos=0;
		if(hour!=pretime.hour||curmenu!=menu)
		{
		   if(hour<10)
			 {
			   OLED_ShowNum(x_pos,4,0,1,32);
				 x_pos+=16;
				 OLED_ShowNum(x_pos,4,hour,1,32);
				 x_pos+=16;
			 
			 
			 }
			 else
			 {
			    OLED_ShowNum(x_pos,4,hour,2,32);
				 x_pos+=32;
			
			 }

		}
		 else
		 {x_pos+=32;}
		
		 OLED_ShowChar(x_pos,4,':',32);
		 x_pos+=16;
		 
		 	if(min!=pretime.min||curmenu!=menu)
		{
		   if(min<10)
			 {
			   OLED_ShowNum(x_pos,4,0,1,32);
				 x_pos+=16;
				 OLED_ShowNum(x_pos,4,min,1,32);
				 x_pos+=16;
			 
			 
			 }
			 else
			 {
			    OLED_ShowNum(x_pos,4,min,2,32);
				 x_pos+=32;
			
			 }

		}
		 else
		 {x_pos+=32;}
		 
		 OLED_ShowChar(x_pos,4,':',32);
		 x_pos+=16;
		 
			 	if(second!=pretime.second||curmenu!=menu)
		{
		   if(second<10)
			 {
			   OLED_ShowNum(x_pos,4,0,1,32);
				 x_pos+=16;
				 OLED_ShowNum(x_pos,4,second,1,32);
				 x_pos+=16;
			 
			 
			 }
			 else
			 {
			    OLED_ShowNum(x_pos,4,second,2,32);
				 x_pos+=32;
			
			 }

		}
		 else
		 {x_pos+=32;}
		
		
		
		pretime.year=year;
		pretime.month=month;
		pretime.day=day;
		pretime.hour=hour;
		pretime.min=min;
		pretime.second=second;
		pretime.week=week;
}
void show_oled()
{
   OLED_Clear(); 
   while(1)
	 {
	   if(menu==1)
	   {  
			 show_Envriment();
			 curmenu=menu;
		 }
	   else if(menu==2)
     { 
			 show_time();
			curmenu=menu;
     }			 
	   osDelay(500);
	 
	 
	 
	 }
    





}